package com.nl.controller.common.redis;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;

/**
 * export all data from rediscluster
 * @author ztl
 *
 */
public class ExampleScanRedis {//hash类型的key


	public void scan (Jedis jedis) {
		    String si = "0";//游标
		    ScanParams sp = new ScanParams();
		    sp.count(500);// 一次扫描的个数
//		    sp.match(pattern);//正则过滤
		    Pipeline pp = null;
		    Map<String, Response<Map<String, String>>> responsees = null;
		    try{
		        pp = jedis.pipelined();
		        responsees = new HashMap<String, Response<Map<String, String>>>();
		    } catch (Exception e) {
		        try{
		            pp = jedis.pipelined();
		            responsees = new HashMap<String, Response<Map<String, String>>>(); 
		        } catch(Exception e1) {
		        }
		    }
		    do {
		        ScanResult<String> srt = null;
		        Iterator<String> it = null;
		        try{
		            srt = jedis.scan(si, sp);
		            for (String key : srt.getResult()) {
		                if (!key.contains("_")) {
		                    responsees.put(key, pp.hgetAll(key));
		                }
		            }
		            pp.sync();
		            it = responsees.keySet().iterator();
		        } catch (Exception e) {
		        }
		        if (null != it) {
		            while(it.hasNext()){
		                String key = it.next();
		                try {
//		                    Map<String, String> map = responsees.get(key).get();
		                } catch (Exception e) {
		                }
		            }  
		        }
		        if (null != srt) {
		           si = srt.getStringCursor();
		        }
		        responsees.clear();
		    } while (!si.equals("0"));
		    jedis.close();

	}
   
}
